import { createRouter, createWebHistory, RouteRecordRaw } from "vue-router";
import layoutMain from "@/views/main.vue";

const routes: Array<RouteRecordRaw> = [
    {
        path: "/",
        name: "layoutMain",
        redirect: "/index",
        component: layoutMain,
        children: [
            {
                path: "/index",
                name: "index",
                component: () => import("@/views/HomeView.vue"),
            },
        ],
    },
    {
        path: "/login",
        name: "loginWeb",
        meta: { title: "登录" },
        component: () => import("@/views/user/login.vue"),
    },
    {
        path: "/user",
        name: "userWeb",
        component: layoutMain,
        children: [
            {
                path: "stu",
                name: "stuList",
                meta: { title: "学生管理" },
                component: () => import("@/views/user/student.vue"),
            },
            {
                path: "log",
                name: "userLog",
                meta: { title: "用户日志" },
                component: () => import("@/views/user/log.vue"),
            },
            {
                path: "file",
                name: "userFile",
                meta: { title: "文件管理" },
                component: () => import("@/views/user/file.vue"),
            },
            {
                path: "region",
                name: "regionWeb",
                meta: { title: "区域数据" },
                component: () => import("@/views/user/region.vue"),
            },
        ],
    },
];

const router = createRouter({
    history: createWebHistory(process.env.BASE_URL),
    routes,
});

router.beforeEach(async (to, from, next) => {
    document.title =
        (to.meta && to.meta.title ? to.meta.title + " - " : "") +
        process.env.VUE_APP_PROJECT_NAME;
    next();
});

router.afterEach((to) => {
    window.scrollTo(0, 0);
});

export default router;
